package com.zara.store.common;

import java.io.IOException;
import java.math.BigDecimal;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;
import javax.xml.transform.TransformerException;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;


public class PalcWsClient {

	private String wsUrl = "http://localhost:8080/axis/services/PALC";
//	static String WS_URL = "http://5.41.248.65:8080/axis/services/PALC";
	
	public boolean processPalc(PalcVO palcVO) throws TransformerException, IOException, ServiceException {
		String xmlVO = new PalcHelper().createXmlFromVO(palcVO);
		String xml = new IntegrationTransformer()
				.transform(xmlVO);
		return sendPalc(xml);
	}

	public boolean sendPalc(String xml) throws ServiceException, RemoteException {
		Service service = new Service();
		Call call = (Call) service.createCall();
		FileHelper helper = new FileHelper("c:/zara-workspace/zara.process.palc.log");
		helper.addLine("Setting PaLC endpoint: " + wsUrl);
		call.setTargetEndpointAddress(wsUrl);
		call.addParameter("xml", XMLType.XSD_STRING, ParameterMode.IN);
		call.setReturnType(XMLType.XSD_INT);
		call.setOperationName("setPALC");
		
		helper.addLine("Invoke PaLC");
		Object ret = call.invoke(new Object[] { xml });
		helper.addLine("PaLC reply: " + ret);
		return true;
	}

	public String getWsUrl() {
		return wsUrl;
	}

	public void setWsUrl(String wsUrl) {
		this.wsUrl = wsUrl;
	}

	public static void main(String[] args) throws Exception {
		PalcVO palcVO = new PalcVO();
		palcVO.setDate(new Date());
		palcVO.setId(10);
		List<PalcItemVO> items = new ArrayList<PalcItemVO>();
		PalcItemVO palcItemVO = new PalcItemVO(1, BigDecimal.TEN);
		items.add(palcItemVO);
		palcVO.setItems(items);
		PalcVO palcVO2 = StoreProxy.getInstance().createPalc(items);
		new PalcWsClient().processPalc(palcVO2);
	}
	
}